<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
        <template slot="mainNameForm" slot-scope="scope">
          <avue-select v-model="form.mainName"
            placeholder="请选择主类型名称"
            :dic="scope.dic"
            :props="props"
            :disabled="scope.disabled"
            @change="mainNameChange(arguments, scope.dic)">
          </avue-select>
        </template>
        <template slot='menuLeft'>
          <el-upload
                  class="upload-inline"
                  action=""
                  :show-file-list="false"
                  accept=".xls,.xlsx"
                  :http-request="handleImport"
                 >
                  <el-button type="primary" size="small" v-if="false">导入</el-button>
                </el-upload>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {addObj, putObj,batchImport} from '@/api/support/swpmticketcardtypeparam'

export default {
  name: 'swpmticketcardtypeparam',
  mixins: [baseCrud('support/swpmticketcardtypeparam')],
  data() {
    return {
      props: {
        label: "mainName",
        value: "mainName"
      }
    }
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.support_swpmticketcardtypeparam_add, false),
        delBtn: this.vaildData(this.permissions.support_swpmticketcardtypeparam_del, false),
        editBtn: this.vaildData(this.permissions.support_swpmticketcardtypeparam_del, false)
      };
    }
  },
  methods: {
    handleImport(item){
              let file = item.file
              let param = new FormData()
              param.append('file',file)
              batchImport(param).then(() => {
                this.$notify({
                  title: '成功',
                  message: '导入成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList(this.page)
              })
    },
    rowUpdate: function(row, index, done, loading) {
      if (!this.validateAmountOrCount()) {
        loading();
        return
      }
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    rowSave: function(row, done, loading) {
      if (!this.validateAmountOrCount()) {
        loading();
        return
      }
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    validateAmountOrCount() {
      if (this.validatenull(this.form.ticketAmount) && this.validatenull(this.form.ticketCount)) {
        this.$message.warning('车票金额和车票次数必须填写一个')
        return false
      }
      // if (!this.validatenull(this.form.ticketAmount) && !this.validatenull(this.form.ticketCount)) {
      //   this.$message.warning('车票金额和车票次数只能填写一个')
      //   return false
      // }
      return true
    },
    mainNameChange(args, dic) {
      if (!args[0]) {
        this.form.mainEnName = ''
        return
      }
      if (!dic) {
        return
      }
      for (var d of dic) {
        if (d.mainName == args[0]) {
          this.form.mainEnName = d.mainEnName
          break
        }
      }
    }
  }
}
</script>
